package com.cloudwise.sdg.function;

import com.cloudwise.sdg.constant.RandomConstant;
import com.cloudwise.sdg.utils.ResourceUtils;

import java.util.Random;
import java.util.UUID;

/**
 * 内置函数
 *
 * @author www.toushibao.com
 */
public class BuildInFuncs {
    private static Random random = new Random();

    /**
     * @return ms
     */
    public static long timestamp() {
        return System.currentTimeMillis();
    }

    public static int intRand() {
        return Math.abs(random.nextInt());
    }

    public static long longRand() {
        return Math.abs(random.nextLong());
    }

    public static String uuid() {
        return UUID.randomUUID().toString();
    }

    /**
     * 生成n位随机数
     *
     * @param n
     * @return
     */
    public static String numRand(Integer n) {
        StringBuilder strb = new StringBuilder(n);
        for (int i = 0; i < n; i++) {
            strb.append((char) ('0' + random.nextInt(10)));
        }
        return strb.toString();
    }

    /**
     * 生成n位随机字符串,包括数据，大小写字母
     *
     * @param n
     * @return
     */
    public static String strRand(Integer n) {
        StringBuilder strb = new StringBuilder(n);
        for (int i = 0; i < n; i++) {
            int type = random.nextInt(3);
            switch (type) {
                // 数字
                case 0:
                    strb.append((char) ('0' + random.nextInt(10)));
                    break;

                // 小写
                case 1:
                    strb.append((char) ('a' + random.nextInt(26)));
                    break;

                // 大写
                case 2:
                    strb.append((char) ('A' + random.nextInt(26)));
                    break;
            }
        }
        return strb.toString();
    }

    /**
     * @param n
     * @return 0(included) - n(excluded)
     */
    public static int intRand(Integer n) {
        return random.nextInt(n);
    }

    /**
     * @param s
     * @param e
     * @return
     */
    public static int intRand(Integer s, Integer e) {
        int interval = e - s;
        return s + random.nextInt(interval);
    }

    /**
     * @return 0 - 1.0
     */
    public static double doubleRand() {
        return random.nextDouble();
    }

    /**
     * @param s 最小值
     * @param e 最大值
     * @param n 位数
     * @return
     */
    public static double doubleRand(Integer s, Integer e, Integer n) {
        double randDouble = random.nextDouble();
        int interval = e - s;
        double r = s + (randDouble * interval);
        return Double.parseDouble(String.format("%." + n + "f", r));
    }

    /**
     * 生成IP
     *
     * @return
     */
    public static String ipRand() {
        int[][] range = {{607649792, 608174079}, // 36.56.0.0-36.63.255.255
                {1038614528, 1039007743}, // 61.232.0.0-61.237.255.255
                {1783627776, 1784676351}, // 106.80.0.0-106.95.255.255
                {2035023872, 2035154943}, // 121.76.0.0-121.77.255.255
                {2078801920, 2079064063}, // 123.232.0.0-123.235.255.255
                {-1950089216, -1948778497}, // 139.196.0.0-139.215.255.255
                {-1425539072, -1425014785}, // 171.8.0.0-171.15.255.255
                {-1236271104, -1235419137}, // 182.80.0.0-182.92.255.255
                {-770113536, -768606209}, // 210.25.0.0-210.47.255.255
                {-569376768, -564133889}, // 222.16.0.0-222.95.255.255
        };

        int index = random.nextInt(10);
        String ip = num2ip(range[index][0] + random.nextInt(range[index][1] - range[index][0]));
        return ip;
    }

    /*
     * 将十进制转换成IP地址
     */
    private static String num2ip(int ip) {
        int[] b = new int[4];
        String x = "";
        b[0] = (int) ((ip >> 24) & 0xff);
        b[1] = (int) ((ip >> 16) & 0xff);
        b[2] = (int) ((ip >> 8) & 0xff);
        b[3] = (int) (ip & 0xff);
        x = Integer.toString(b[0]) + "." + Integer.toString(b[1]) + "." + Integer.toString(b[2]) + "." + Integer.toString(b[3]);

        return x;
    }

    /**
     * 生成随机的域名
     *
     * @param maxLength 域名最大长度
     * @return 随机域名
     */
    public static String domainRand(Integer maxLength) {
        String domain = strRand(maxLength) + "." +
                ResourceUtils.getRandomElement(RandomConstant.domainSuffixList);
        return domain.toLowerCase();
    }

    public static void main(String[] args) {
        System.out.println(intRand(10));
        System.out.println(intRand(1025, 65535));
        System.out.println(doubleRand());
        System.out.println(doubleRand(10, 100, 3));
        System.out.println(numRand(10));
        System.out.println(strRand(10));
        System.out.println(ipRand());
        System.out.println(domainRand(6));
    }
}
